package com.xuliugen.ssm.demo.controller;

import com.xuliugen.ssm.demo.common.Response;
import com.xuliugen.ssm.demo.controller.base.BaseController;
import com.xuliugen.ssm.demo.dto.SecurityUserDTO;
import com.xuliugen.ssm.demo.facade.SecurityUserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;

/**
 * 用户控制器
 * Created by xuliugen on 2017/5/2.
 */
@RestController
public class SecurityUserController extends BaseController {

    @Autowired
    private SecurityUserFacade securityUserFacade;

    @PostMapping(value = "/add")
    public Response addUser(@RequestBody SecurityUserDTO userDTO) {
        return securityUserFacade.addUser(userDTO);
    }

    /**
     * 更新用户信息
     * @param userDTO
     * @return
     */
    @PatchMapping(value = "/update")
    public Response updateUser(@RequestBody SecurityUserDTO userDTO) {
        return securityUserFacade.updateUser(userDTO);
    }

    /**
     * 添加用户的时候判断用户名是否已经存在
     * @param userName 用户名,不是用户真实姓名：限制用户必须是字母和数字或者'.'的组合，不能包含中文
     * @return
     */
    @GetMapping(value = "/getby/name")
    public Response getUserByUserName(@RequestParam("userName") String userName) {
        return securityUserFacade.getUserByUserName(userName);
    }

    /**
     * 用户删除
     * @param userId
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Response delete(@RequestParam("userId") Integer userId) {
        return securityUserFacade.delete(userId);
    }

    /**
     * 根据用户ID查找用户信息
     * @param userId 用户ID
     * @return
     */
    @GetMapping(value = "/get")
    public Response getUserById(Integer userId) {
        return securityUserFacade.getUserById(userId);
    }
}
